package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqHoliday;
import cloud.maque.admin.mapper.MqHolidayMapper;
import cloud.maque.admin.service.MqHolidayService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author 4color
 * @description mq_holiday
 * @date 2023-03-17
 */
@Service
public class MqHolidayServiceImpl extends ServiceImpl<MqHolidayMapper, MqHoliday> implements MqHolidayService {

    @Override
    public MqHoliday insert(MqHoliday holiday) {
        Assert.hasText(holiday.getHolidayName(), "假期名称不能为空");
        save(holiday);
        return holiday;
    }

    @Override
    public PageInfo<MqHoliday> page(PaginateDataResquest<MqHoliday> pageReq) {

        PageHelper.startPage(pageReq.getPageIndex(), pageReq.getPageSize());
        QueryWrapper<MqHoliday> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getHolidayName())) {
            wrapper.lambda().like(MqHoliday::getHolidayName, pageReq.getData().getHolidayName());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantId())) {
            wrapper.lambda().eq(MqHoliday::getTenantId, pageReq.getData().getTenantId());
        }

        wrapper.lambda().orderByAsc(MqHoliday::getHolidayDay);
        wrapper.lambda().orderByAsc(MqHoliday::getHolidayId);
        List<MqHoliday> lists = this.baseMapper.selectList(wrapper);
        PageInfo<MqHoliday> pageInfo = new PageInfo<>(lists);

        return pageInfo;
    }
}
